#!/usr/bin/env python
# -*- coding:utf-8 -*-
import re

from pyquery import PyQuery

from base.gsxt_base_worker import GsxtBaseWorker
from common import util
from common.global_field import Model

'''
1. 搜索没有结果判断功能添加
'''


class GsxtNeiMengGuWorker(GsxtBaseWorker):
    def __init__(self, **kwargs):
        GsxtBaseWorker.__init__(self, **kwargs)

    def get_search_result_html(self, keyword, session):

        # return [{
        #     'url': 'http://nm.gsxt.gov.cn/GSpublicity/GSpublicityList.html?service=entInfo_c4Frs7b'
        #            '+2ZQqW0yb4yveiqS0bED8VRmYt6WcNLOAGVgwVFlsxbpk35aHFZRmFvJH-zdT5gKc46kfK4GlgRrhUVA=='}], \
        #        self.SEARCH_SUCCESS

        param_list = []
        try:
            url = 'http://{host}'.format(host=self.host)
            content = self.get_captcha_geetest(url, '#content', '#search', keyword, '.mianBodyStyle',
                                               proxy_type=2)
            if content is None:
                return param_list, self.SEARCH_ERROR

            jq = PyQuery(content, parser='html')
            if jq.find('div.textStyle').find('span').text() == '0':
                return param_list, self.SEARCH_NOTHING_FIND

            item_list = jq.find('.mianBodyStyle').find('.clickStyle').find('a').items()
            for item in item_list:
                href = item.attr('href')
                if href is None or href == '':
                    continue
                if href.find('..') != -1:
                    url = 'http://{host}'.format(host=self.host) + href[2:]
                else:
                    url = href
                param_list.append({'url': url})
        except Exception as e:
            self.log.exception(e)
            return param_list, self.SEARCH_ERROR

        return param_list, self.SEARCH_SUCCESS if len(param_list) > 0 else self.SEARCH_ERROR

    @staticmethod
    def get_company_name_new(text):
        if text == '':
            return None
        jq = PyQuery(text, parser='html')
        company = jq.find('.conpanyInfo').find('li').eq(0).find('span').eq(0).text()

        return company.strip()

    @staticmethod
    def get_key_word_new(text):
        if text == '':
            return

        jq = PyQuery(text, parser='html')
        ent_no = jq.find('#entNo').attr('value')
        ent_type = jq.find('#entType').attr('value')
        reg_org = jq.find('#regOrg').attr('value')

        key_info = {
            'entNo': ent_no,
            'entType': ent_type,
            'regOrg': reg_org
        }

        return key_info

    # 出资信息
    def get_contributive_info_new(self, host, session, data, key_info):

        url = 'http://{host}//invInfo/invInfoList?pageNo=1&entNo={entNo}&regOrg={regOrg}'.format(
            entNo=key_info['entNo'], regOrg=key_info['regOrg'], host=host)
        r = self.task_request(session.get, url)
        if r is None:
            return

        json_data = util.json_loads(r.text)
        if json_data is None:
            return

        # 数据可用,先发送
        self.append_model(data, Model.contributive_info, url, r.text)

        json_list = json_data.get('list', None)
        if json_list is None:
            return

        total_page = json_list.get('totalPages', 0)
        for page in xrange(2, total_page + 1):
            url = 'http://{host}//invInfo/invInfoList?pageNo={page}&entNo={entNo}&regOrg={regOrg}'.format(
                entNo=key_info['entNo'], regOrg=key_info['regOrg'], page=page, host=host)
            r = self.task_request(session.get, url)
            if r is None:
                return

            self.append_model(data, Model.contributive_info, url, r.text)

        json_list = json_list.get('list', None)
        if json_list is None:
            return

        for item in json_list:
            inv_no = item.get('invNo', None)
            ent_no = item.get('entNo', None)

            url = 'http://{host}/GSpublicity/invInfoDetails.html?invNo={invNo}&entNo={entNo}&regOrg={regOrg}'.format(
                host=host, invNo=inv_no, entNo=ent_no, regOrg=key_info['regOrg'])
            r = self.task_request(session.get, url)
            if r is None:
                continue

            self.append_model(data, Model.contributive_info, url, r.text, classify=Model.type_detail)

    # 获得主要人员信息
    def get_key_person_info_new(self, host, session, data, key_info):
        url = 'http://{host}//vip/vipList.html?entNo={entNo}&regOrg={regOrg}'.format(
            host=host, entNo=key_info['entNo'], regOrg=key_info['regOrg'])
        r = self.task_request(session.get, url)
        if r is None:
            return

        self.append_model(data, Model.key_person_info, url, r.text)

    # 获得分支机构信息
    def get_branch_info_new(self, host, session, data, key_info):
        url = 'http://{host}//CipBraInfo/cipBraInfoList.html?entNo={entNo}&regOrg={regOrg}'.format(
            host=host, entNo=key_info['entNo'], regOrg=key_info['regOrg'])
        r = self.task_request(session.get, url)
        if r is None:
            return

        self.append_model(data, Model.branch_info, url, r.text)

    # 股东信息
    def get_shareholder_info_new(self, host, session, data, key_info):
        url = 'http://{host}//REIInvInfo/REIInvInfoList?pageNo=1&entNo={entNo}&regOrg={regOrg}'.format(
            entNo=key_info['entNo'], regOrg=key_info['regOrg'], host=host)
        r = self.task_request(session.get, url)
        if r is None:
            return

        json_data = util.json_loads(r.text)
        if json_data is None:
            return

        # 数据可用,先发送
        self.append_model(data, Model.shareholder_info, url, r.text)

        json_list = json_data.get('list', None)
        if json_list is None:
            return

        total_page = json_list.get('totalPages', 0)
        for page in xrange(2, total_page + 1):
            url = 'http://{host}//REIInvInfo/REIInvInfoList?pageNo={page}&entNo={entNo}&regOrg={regOrg}'.format(
                entNo=key_info['entNo'], regOrg=key_info['regOrg'], page=page, host=host)
            r = self.task_request(session.get, url)
            if r is None:
                return

            self.append_model(data, Model.shareholder_info, url, r.text)

    # 获取年报信息
    def get_annual_info_new(self, host, session, data, text, key_info):
        search_list = re.findall('onclick="reportview\((.*?),.*?\)"', text)
        for year in search_list:
            url = 'http://{host}//BusinessAnnals/annualReport.html?entNo={entNo}&reportYear={year}&entityType=1&regOrg={regOrg}'.format(
                host=host, entNo=key_info['entNo'], year=year, regOrg=key_info['regOrg'])
            r = self.task_request(session.get, url)
            if r is None:
                continue
            self.append_model(data, Model.annual_info, url, r.text, year=year, classify=Model.type_detail)

            # 最多翻10页
            for page in xrange(1, 10):
                url = 'http://{host}//capital/capitalList?pageNo={page}&entNo={entNo}&regOrg={regOrg}&reportYear={year}'.format(
                    host=host, page=page, entNo=key_info['entNo'], year=year, regOrg=key_info['regOrg'])
                r = self.task_request(session.get, url)
                if r is None:
                    continue
                if len(r.text) <= 15:
                    break
                self.append_model(data, Model.annual_info, url, r.text, year=year, classify=Model.type_detail)

    def get_change_info_new(self, host, session, data, key_info):
        # url = 'http://nm.gsxt.gov.cn//EntChaInfo/EntChatInfoList?pageNo=1&entNo=fb25e1fd-a52b-4c17-a5fe-003ee5204fdb&regOrg=152900'

        url = 'http://{host}//EntChaInfo/EntChatInfoList?pageNo=1&entNo={entNo}&regOrg={regOrg}'.format(
            entNo=key_info['entNo'], regOrg=key_info['regOrg'], host=host)
        r = self.task_request(session.get, url)
        if r is None:
            return

        json_data = util.json_loads(r.text)
        if json_data is None:
            return

        # 数据可用,先发送
        self.append_model(data, Model.change_info, url, r.text)

        json_list = json_data.get('list', None)
        if json_list is None:
            return

        total_page = json_list.get('totalPages', 0)
        for page in xrange(2, total_page + 1):
            url = 'http://{host}//EntChaInfo/EntChatInfoList?pageNo={page}&entNo={entNo}&regOrg={regOrg}'.format(
                entNo=key_info['entNo'], regOrg=key_info['regOrg'], page=page, host=host)
            r = self.task_request(session.get, url)
            if r is None:
                return

            self.append_model(data, Model.change_info, url, r.text)

    # 新版方式
    def get_detail_html_list_new(self, host, seed, session, detail_url):
        # 获得基本信息
        r = self.task_request(session.get, detail_url)
        if r is None:
            return None

        company_name = self.get_company_name_new(r.text)
        if company_name is None or company_name == '':
            return None

        data = self.get_model(company_name, 0, seed, self.province)

        # 保存基础信息
        self.append_model(data, Model.base_info, detail_url, r.text)

        # 获得关键字段
        key_info = self.get_key_word_new(r.text)

        # 变更信息
        self.get_change_info_new(host, session, data, key_info)

        # 获得出资信息
        self.get_contributive_info_new(host, session, data, key_info)

        # 获得主要人员信息
        self.get_key_person_info_new(host, session, data, key_info)

        # 获得分支机构信息
        self.get_branch_info_new(host, session, data, key_info)

        # 获得股东信息
        self.get_shareholder_info_new(host, session, data, key_info)

        # 年报信息
        self.get_annual_info_new(host, session, data, r.text, key_info)

        return data

    def get_detail_html_list(self, seed, session, param_list):

        data_list = []
        rank = 0
        for item in param_list:
            try:
                detail_url = item.get('url', None)
                data = self.get_detail_html_list_new(self.host, seed, session, detail_url)
                if data is not None:
                    rank += 1
                    data['rank'] = rank
                    data_list.append(data)
            except Exception as e:
                self.log.exception(e)
        self.target_db.insert_batch_data(self.target_table, data_list)
        return len(data_list)
